###Diplomatic Statements and the Strategic Use of Terrorism in Civil Wars###
###Levy, Dudley, Chen, Siegel###
###Journal of Conflict Resolution###
###R Script 1: Table 1 & Figure 1a-1b in Manuscript###

rm(list = ls())

#setwd("Replication")

library(dplyr)
library(lme4)
library(texreg)
library(ggplot2)
library(coefplot)
library(stargazer)
library(merTools)
library(ggeffects)
library(ggpubr)
library(gridGraphics)
library(patchwork)
library(egg)
library(ggpubr)
library(estimatr)
library(lubridate)
library(xtable)
library(Hmisc)
library(pastecs)
source("Code/functions.R")
load("Data/df.RData")

####Table 1####

l2.prop <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                prev_res_reb_lag3 + prev_res_gov_lag3 +
                any_reb_int_lag2 + any_gov_int_lag2 +
                physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                coldwar +rebels_count + duration,
              data = df)

l2.propXP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_X_d_lag2 + gov_X_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                  coldwar +rebels_count + duration,
                data = df)

l2.propWP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_W_d_lag2 + gov_W_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar +rebels_count + duration,
                data = df)

l2.propEP <- lm(CivAttackProp ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_E_d_lag2 + gov_E_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar +rebels_count + duration,
                data = df)

screenreg(list(l2.prop, l2.propXP, l2.propWP, l2.propEP),
          custom.header = list("Any Material Support" = 1,
                               "Disaggregated Material Support" = 2:4),
          stars = c(0.01, 0.05, 0.1), digits = 3) 

texreg(list(l2.prop, l2.propXP, l2.propWP, l2.propEP), file = "Tables/Table_1.tex",
       stars = c(0.01, 0.05, 0.1),
       caption = "Linear Regression Results for Attacks against Civilians in Civil Wars",
       caption.above = TRUE,
       label = "tab:tab1",
       use.packages = FALSE,
       booktabs = TRUE,
       scalebox = 0.8,
       custom.header = list("Any Material Support" = 1,
                            "Disaggregated Material Support" = 2:4),
       custom.model.names = c("Model 1", "Model 2", "Model 3", "Model 4"),
       custom.coef.map = list("CivAttackProp_lag" = "Lagged DV (Prop Attacks Against Civilians)",
                              "pro_reb_res_lag2" = "Pro-Rebel Resolution", 
                              "pro_gov_res_lag2" = "Pro-Government Resolution",
                              "prev_res_reb_lag3" = "Previous Pro Reb Res,Count", 
                              "prev_res_gov_lag3" = "Previous Pro Gov Res,Count", 
                              "any_reb_int_lag2"= "Any Pro-Rebel Support",
                              "any_gov_int_lag2"= "Any Pro-Government Support",
                              "reb_X_d_lag2" = "Pro Reb Intervention, Troops", 
                              "gov_X_d_lag2" = "Pro Gov Intervention, Troops", 
                              "reb_W_d_lag2" = "Pro Reb Intervention, Weapons",
                              "gov_W_d_lag2" = "Pro Gov Intervention, Weapons", 
                              "reb_E_d_lag2" = "Pro Reb Intervention, Economic", 
                              "gov_E_d_lag2" = "Pro Gov Intervention, Economic", 
                              "physint" = "Physical Integrity Rights", 
                              "lngdppc" = "Logged GDP per capita", 
                              "lntpop" = "Logged Population",
                              "rebstrength2" = "Rebel Strength",  
                              "coldwar" = "Cold War",
                              "rebels_count" = "Count of Rebel Groups",
                              "duration" = "Conflict Duration",
                              "(Intercept)" = "Intercept"),
       digits = 3)

####Figure 1A####

p1 <- LM_maginal_plot(Fits = list(l2.prop, l2.propXP, l2.propWP, l2.propEP), 
                      n_sim = 1000, var = c("pro_reb_res_lag2",
                                            "pro_reb_res_lag2",
                                            "pro_reb_res_lag2",
                                            "pro_reb_res_lag2"), val1 = 0, val2 = 1,
                      data = df, modname = c("Model 1: Any Material Support",
                                             "Model 2: Troops Intervention", 
                                             "Model 3: Weapons Intervention",
                                             "Model 4: Economic Intervention"))
fig1_a <- p1$plot +  xlab("Average marginal effects") + ggtitle("Pro-Rebel Resolution") 

ggsave("Figures/Figure1a.pdf", 
       plot = fig1_a, dpi = 600, width = 6, height = 4)


####Figure 1B####

p2 <- LM_maginal_plot(Fits = list(l2.prop, l2.propXP, l2.propWP, l2.propEP), 
                      n_sim = 1000, var = c("any_reb_int_lag2",
                                            "reb_X_d_lag2",
                                            "reb_W_d_lag2",
                                            "reb_E_d_lag2"), val1 = 0, val2 = 1,
                      data = df, modname = c("Model 1: Any Material Support",
                                             "Model 2: Troops Intervention", 
                                             "Model 3: Weapons Intervention",
                                             "Model 4: Economic Intervention"))
fig1_b <- p2$plot +  xlab("Average marginal effects") + ggtitle("Pro-Rebel Material Support") 
ggsave("Figures/Figure1b.pdf", 
       plot = fig1_b, dpi = 600, width = 6, height = 4)

